<?php
defined('_JEXEC') or die( 'Restricted access' );
jimport('joomla.application.component.model');

class ContentModelContentlist extends JModel{
    
    function getList(){
        //$arg = func_get_args();
        $post = JRequest::get('get');
        if(!empty($post['section']))
        {
            $list = $this->mySection($post['section']);
        }
        else if(!empty($post['category']))
        {
            $categorys = explode(':',$post['category']);
            $list = $this->myCategory($categorys[0]);
        }
        return $list;
    }

    function mySection($section){
        $db =& JFactory::getDBO();
        $sql = "SELECT a.id,a.title,a.alias,a.catid,b.alias AS cat_alias,c.title AS head_title "
              ." FROM #__content AS a,"
              ." #__categories AS b,"
              ." #__sections AS c"
              ." WHERE a.sectionid = '".$section."'"
              ." AND a.catid = b.id"
              ." AND a.sectionid = c.id"
              ." ORDER BY a.id DESC"
              ;
        $db->setQuery($sql);
        $res = $db->loadObjectList();
        $res = $this->myRoute($res);
        return $res;
    }

    function myCategory($category){
        $db =& JFactory::getDBO();
        $sql = "SELECT a.id,a.title,a.alias,a.catid,b.alias AS cat_alias,b.title AS head_title "
              ." FROM #__content AS a,"
              ." #__categories AS b"
              ." WHERE a.catid = '".$category."'"
              ." AND a.catid = b.id"
              ." ORDER BY id DESC"
              ;
        $db->setQuery($sql);
        $res = $db->loadObjectList();
        $res = $this->myRoute($res);
        return $res;
    }

    function myRoute($res){
        for($i=0; $i<count($res); $i++){
            $article = $res[$i];

            $id = $article->id.':'.$article->alias;
            $catid = $article->catid.':'.$article->cat_alias;
            $sectionid = $article->sectionid;
            
            $article->link = JRoute::_(ContentHelperRoute::getArticleRoute($id, $catid, $sectionid));
            $callBack[] = $article;
        }

        return $callBack;
    }
}

?>
